package com.gcph.zhuoba.main.activitys;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gcph.zhuoba.R;
import com.gcph.zhuoba.main.common.Constant;
import com.gcph.zhuoba.util.L;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

public class ResourceInputActivity extends Activity {
	private Long parentResourceId;
	private int resourceType;
	private String resourceData;
	private String resourceLine;
	private String resourceDesc;
	private int imgFileId;
	private int videoFileId;
	private int resourceSmallType;
	private int isPub;
	private int star;
	
	private ImageView input_money_title;
	private LinearLayout input_money_lin;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.resource_input);
		Intent intent = getIntent();
		parentResourceId =  intent.getLongExtra("parentResourceId",0L);
		resourceType = intent.getIntExtra("resourceType", Constant.RESOURCE_TYPE_JIAOAN);//类型
		imgFileId = intent.getIntExtra("imgFileId", 0);//球型图片文件ID
		resourceData = intent.getStringExtra("resourceData");//球型数据
		resourceLine = intent.getStringExtra("resourceLine");//线路数据
		videoFileId = intent.getIntExtra("videoFileId",0);//视频文件ID
		
		L.d("在ResourceInputActivity中接到===imgFileId="+imgFileId+"=====videoFileId="+videoFileId);
		resourceSmallType = intent.getIntExtra("resourceSmallType",Constant.RESOURCE_SMALL_TYPE_ZOUWEI);//小分类
		isPub = intent.getIntExtra("isPub",Constant.GLOBAL_YESNO_YES);//是否发布
		star = intent.getIntExtra("star",0);//难度
		// TODO 得到当前登录用户
		
		//设置金额是否显现
		ImageView input_money_title=(ImageView)findViewById(R.id.input_money_title); 
		LinearLayout input_money_lin=(LinearLayout)findViewById(R.id.input_money_lin); 
		if(Constant.RESOURCE_TYPE_QIUZHU.equals(resourceType))
		{
			input_money_title.setVisibility(View.VISIBLE);
			input_money_lin.setVisibility(View.VISIBLE);
		}else
		{
			input_money_title.setVisibility(View.INVISIBLE);
			input_money_lin.setVisibility(View.INVISIBLE);
		}

		// 设置标题
		TextView title = (TextView) findViewById(R.id.resource_input_title);
		if (resourceType == Constant.RESOURCE_TYPE_JIAOAN)
			//title.setText(R.string.resource_input_title_jiaoan);
		if (resourceType == Constant.RESOURCE_TYPE_LIANXI)
			title.setText(R.string.resource_input_title_lianxi);
		if (resourceType == Constant.RESOURCE_TYPE_QIUZHU)
			title.setText(R.string.resource_input_title_qiuzhu);
		if (resourceType == Constant.RESOURCE_TYPE_JIEDA)
			title.setText(R.string.resource_input_title_jieda);
		//设置返回按钮事件
		ImageButton back=(ImageButton)findViewById(R.id.btn_back); 
		back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				setResult(1,null);
				ResourceInputActivity.this.finish();
			}
		});
		//设置发布按钮事件
		Button pub=(Button)findViewById(R.id.btn_publish); 
		pub.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//设置描述文字
				EditText descEditText=(EditText)findViewById(R.id.resource_input_desc);
				Editable descEditable=descEditText.getText();
				resourceDesc=descEditText.getText().toString();
				if(resourceDesc==null||"".equals(resourceDesc)){
					Toast toast=Toast.makeText(getApplicationContext(),R.string.publish_failed_input_desc, Toast.LENGTH_SHORT);
					toast.show();
					return;
				}
				//保存资源信息
				HttpUtils http = new HttpUtils();
				RequestParams params=new RequestParams();
				if(0!=parentResourceId.intValue())
				{
					params.addBodyParameter("resource.id", String.valueOf(parentResourceId));
				}
				
				params.addBodyParameter("resourceType", String.valueOf(resourceType));
				params.addBodyParameter("imgFileId", String.valueOf(imgFileId));
				params.addBodyParameter("resourceData", resourceData);
				params.addBodyParameter("resourceLine", resourceLine);
				params.addBodyParameter("videoFileId", String.valueOf(videoFileId));
				params.addBodyParameter("resourceSmallType", String.valueOf(resourceSmallType));
				params.addBodyParameter("isPub", String.valueOf(isPub));
				params.addBodyParameter("star", String.valueOf(star));
				params.addBodyParameter("resourceDesc",resourceDesc);
				
				//添加赏金
				if(Constant.RESOURCE_TYPE_QIUZHU.equals(resourceType))
				{
					EditText input_money=(EditText)findViewById(R.id.input_money);
					params.addBodyParameter("money",input_money.getText().toString());
				}
				
				L.d("resource/save===parentResourceId="+parentResourceId);
				L.d("resource/save===resourceLine="+resourceLine);

				http.send(HttpRequest.HttpMethod.POST, getResources().getString(R.string.api_resource_save),params,
						new RequestCallBack<String>() {
							@Override
							public void onSuccess(ResponseInfo<String> responseInfo) {
								String response = responseInfo.result;
								if (response == null || "".equals(response))
									return;
								//提示发布成功，跳转到上一级
								Toast toast=Toast.makeText(getApplicationContext(),R.string.publish_success, Toast.LENGTH_SHORT);
								toast.show();
								
								setResult(0,null);
								finish();
							}

							@Override
							public void onFailure(HttpException error, String msg) {
								Toast toast=Toast.makeText(getApplicationContext(),R.string.publish_failed+"："+msg, Toast.LENGTH_SHORT);
								toast.show();
							}
						});
				
			}
		});
	}
}
